using System;
using System.IO;
using System.Xml.Serialization;
using Widgetsphere.Generator.Common.Logging;
using Widgetsphere.Generator.Common.Util;

namespace Widgetsphere.Generator.Common.GeneratorFramework
{
	public class AddinAppData
	{
		#region Class Members

		private const string DATA_FILE_NAME = "addInStore.xml";
		private const string COMPANY_FOLDER = "Widgetsphere";
		private const string REG_LOCATION = @"Software\Widgetsphere\VSCodeTools";
		private const string REG_LOCATION2 = @"Software\Widgetsphere\nHydrate";
		private const string REG_INSTALLDIR_PROPERTY = "InstallDir";

		private static readonly AddinAppData _instance = null;
		private readonly FileInfo _addinDataStoreFile = null;
		private readonly AddinProperties _addInProperties = null;

		public enum nHydrateVersion
		{
			v4,
			v5,
		}

		#endregion

		#region Constructors

		static AddinAppData()
		{
			try
			{
				_instance = new AddinAppData();
				_instance.Version = nHydrateVersion.v4;
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		private AddinAppData()
		{
			var fullFileName = string.Empty;
			try
			{
				var appDataFolder = StringHelper.EnsureDirectorySeperatorAtEnd(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData));
				fullFileName = string.Format("{0}{1}/{2}", appDataFolder, COMPANY_FOLDER, DATA_FILE_NAME);

				_addinDataStoreFile = new FileInfo(fullFileName);
				_addInProperties = new AddinProperties();
				if (_addinDataStoreFile.Exists)
				{
					var serializer = new XmlSerializer(typeof(AddinProperties));
					var tr = new StreamReader(_addinDataStoreFile.FullName);
					_addInProperties = (AddinProperties)serializer.Deserialize(tr);
					tr.Close();
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Filename: " + fullFileName, ex);
			}
		}

		#endregion

		#region Properties

		public nHydrateVersion Version { get; set; }

		private AddinProperties Properties
		{
			get { return _addInProperties; }
		}

		public static AddinAppData Instance
		{
			get { return _instance; }
		}

		public string ExtensionDirectory
		{
			get
			{
				if (this.Version == nHydrateVersion.v5)
				{
					//Try for the visual/registered version first
					return RegistryHelper.GetLocalMachineRegistryValue(REG_LOCATION2, REG_INSTALLDIR_PROPERTY);
				}
				else if (this.Version == nHydrateVersion.v4)
				{
					//Try the freeware version second
					return RegistryHelper.GetLocalMachineRegistryValue(REG_LOCATION, REG_INSTALLDIR_PROPERTY);
				}
				else
				{
					throw new Exception("Unknown nHydrate Version");
				}
			}
		}

		public string Key
		{
			get { return Properties.Key; }
			set { Properties.Key = value; }
		}

		public DateTime LastUpdateCheck
		{
			get { return Properties.LastUpdateCheck; }
			set { Properties.LastUpdateCheck = value; }
		}

		public string LastVersionChecked
		{
			get { return Properties.LastVersionChecked; }
			set { Properties.LastVersionChecked = value; }
		}

		public bool AllowStats
		{
			get { return Properties.AllowStats; }
			set { Properties.AllowStats = value; }
		}

		public void Save()
		{
			try
			{
				if (!_addinDataStoreFile.Directory.Exists)
					_addinDataStoreFile.Directory.Create();

				var serializer = new XmlSerializer(typeof(AddinProperties));
				var tw = new StreamWriter(_addinDataStoreFile.FullName);
				serializer.Serialize(tw, Properties);
				tw.Close();
			}
			catch (Exception ex)
			{
				WSLog.LogError(ex);
			}
		}

		#endregion

	}
}